package com.dianrui.operation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dianrui.operation.Constant;
import com.dianrui.operation.R;
import com.dianrui.operation.adapter.ViolationOrderListAdapter;
import com.dianrui.operation.bean.VioOrder;
import com.dianrui.operation.event.RefreshVio;
import com.dianrui.operation.okhttp.XHttpRequest;
import com.dianrui.operation.util.SharedUtil;
import com.dianrui.operation.util.StringUtils;
import com.dianrui.operation.util.ToastUtil;
import com.dianrui.operation.util.Url;
import com.google.gson.JsonObject;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 提交的违章工单
 */
public class ViolationOrderListActivity extends BaseActivity {
    private int mPageIndex=1;
    private List<String> imageList = new ArrayList<>();
    private List<VioOrder> vioOrdersList = new ArrayList<>();
    private ViolationOrderListAdapter violationOrderListAdapter;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    @Override
    public int getContentViewId() {
        return R.layout.violation_order_list;
    }

    @Override
    public void initView() {
        title.setText(getString(R.string.main_menu_violation_txt));
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        violationOrderListAdapter = new ViolationOrderListAdapter(ViolationOrderListActivity.this, vioOrdersList);
        listview.setAdapter(violationOrderListAdapter);
        getViolationOrderList(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getViolationOrderList(true);
                refreshLayout.finishRefresh();
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPageIndex++;
                getViolationOrderList(false);
                refreshLayout.finishLoadMore();
            }
        });
        violationOrderListAdapter.setOnBatteryClickListener(new ViolationOrderListAdapter.BatteryClickListener() {
            @Override
            public void onBatteryClick(int violation_id,String order_code, String number, String title, String remark, String money, String address, String create_time,JSONArray photo) {
                Intent intent=new Intent(ViolationOrderListActivity.this, ViolationDetailsActivity.class);
                Bundle bundle=new Bundle();
                bundle.putInt("violation_id", violation_id);
                intent.putExtras(bundle);
                startActivity(intent);

            }


        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RefreshVio refreshVio) {
        getViolationOrderList(true);
    }

    private void getViolationOrderList(final boolean isRfresh) {
        if (isRfresh) {
            mPageIndex = 1;
        }
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("p", mPageIndex);
        StringBuilder violationBulider=new StringBuilder();
        violationBulider.append(SharedUtil.getUrl()).append(Url.MYORDER_VIOLATION);
        XHttpRequest.getInstance().postRequests(violationBulider.toString(), jsonObject.toString(), new XHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (isRfresh) {
                    vioOrdersList.clear();
                }
                if (!StringUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && !isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (null!=jsonArray && jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        VioOrder vioOrder=new VioOrder();
                                        vioOrder.violation_id = jsonArray.optJSONObject(i).optInt("violation_id");
                                        vioOrder.create_time= jsonArray.optJSONObject(i).optString("create_time");
                                        vioOrder.order_code= jsonArray.optJSONObject(i).optString("order_code");
                                        vioOrder.vehicle_number= jsonArray.optJSONObject(i).optString("vehicle_number");
                                        vioOrder.title= jsonArray.optJSONObject(i).optString("title");
                                        vioOrder.remark= jsonArray.optJSONObject(i).optString("remark");
                                        vioOrder.money= jsonArray.optJSONObject(i).optString("money");
                                        vioOrder.address= jsonArray.optJSONObject(i).optString("address");
                                        vioOrder.create_time= jsonArray.optJSONObject(i).optString("create_time");
                                        vioOrder.state= jsonArray.optJSONObject(i).optString("state");
                                        vioOrder.photo=jsonArray.optJSONObject(i).optJSONArray("photo");
                                        vioOrdersList.add(vioOrder);
                                    }
                                    if (violationOrderListAdapter != null) {
                                        violationOrderListAdapter.refresh(vioOrdersList);
                                    }
                                    refreshLayout.finishRefresh();

                                } else {

                                    ToastUtil.showToast(getString(R.string.no_violation_list));
                                }
                            }else  if ("1001".equals(jsonObject.optString("status"))) {
                                refreshLayout.finishRefresh();
                                ToastUtil.showToast(jsonObject.optString("message"));
                            } else if ("1002".equals(jsonObject.optString("status"))) {
                                refreshLayout.finishRefresh();
                                spUtils.clear();
                                JumpLoginActivitys(LoginActivity.class);
                                finish();
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (vioOrdersList.size() <= 0) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                        emptyView.setVisibility(View.VISIBLE);
                        listview.setVisibility(View.GONE);
                    } else {
                        emptyView.setVisibility(View.GONE);
                        listview.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void failedRequest(String result) {
                refreshLayout.finishLoadMoreWithNoMoreData();
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    @Override
    public void initData() {}




    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;

                default:
                    break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
